package org.example.rzfx.service;

import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Redis缓存服务
 * 提供统一的缓存操作接口
 */
@Service
public class RedisService {
    
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    
    // ============== 基础操作 ==============
    
    /**
     * 设置缓存
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }
    
    /**
     * 设置缓存并设置过期时间
     */
    public void set(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }
    
    /**
     * 获取缓存
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
    
    /**
     * 获取缓存并转换为指定类型
     */
    public <T> T get(String key, Class<T> clazz) {
        Object value = redisTemplate.opsForValue().get(key);
        if (value == null) {
            return null;
        }
        // 如果是字符串，尝试JSON解析
        if (value instanceof String) {
            return JSON.parseObject((String) value, clazz);
        }
        return clazz.cast(value);
    }
    
    /**
     * 删除缓存
     */
    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }
    
    /**
     * 判断key是否存在
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
    
    /**
     * 设置过期时间
     */
    public Boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }
    
    /**
     * 获取过期时间（秒）
     */
    public Long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }
    
    // ============== 计数器操作 ==============
    
    /**
     * 递增
     */
    public Long increment(String key) {
        return redisTemplate.opsForValue().increment(key);
    }
    
    /**
     * 递增指定值
     */
    public Long increment(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }
    
    /**
     * 递减
     */
    public Long decrement(String key) {
        return redisTemplate.opsForValue().decrement(key);
    }
    
    // ============== Hash操作 ==============
    
    /**
     * Hash设置
     */
    public void hSet(String key, String hashKey, Object value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }
    
    /**
     * Hash获取
     */
    public Object hGet(String key, String hashKey) {
        return redisTemplate.opsForHash().get(key, hashKey);
    }
    
    /**
     * Hash删除
     */
    public Long hDelete(String key, Object... hashKeys) {
        return redisTemplate.opsForHash().delete(key, hashKeys);
    }
    
    /**
     * Hash判断字段是否存在
     */
    public Boolean hHasKey(String key, String hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }
    
    // ============== 业务缓存key生成器 ==============
    
    /**
     * 用户信息缓存key
     */
    public static String userCacheKey(Long userId) {
        return "user:info:" + userId;
    }
    
    /**
     * 日志查询缓存key
     */
    public static String logQueryCacheKey(String params) {
        return "log:query:" + params.hashCode();
    }
    
    /**
     * 统计数据缓存key
     */
    public static String statsCacheKey(String type) {
        return "stats:" + type;
    }
    
    /**
     * Dashboard缓存key
     */
    public static String dashboardCacheKey() {
        return "dashboard:overview";
    }
    
    /**
     * 在线用户缓存key
     */
    public static String onlineUserKey(Long userId) {
        return "online:user:" + userId;
    }
    
    /**
     * 接口访问计数key
     */
    public static String apiCountKey(String apiPath) {
        return "api:count:" + apiPath;
    }
}

